<?php

define('LOG__EMERGENCY',    'emergency');
define('LOG__ALERT',        'alert');
define('LOG__CRITICAL',     'critical');
define('LOG__ERROR',        'error');
define('LOG__WARNING',      'warning');
define('LOG__NOTICE',       'notice');
define('LOG__INFO',         'info');
define('LOG__DEBUG',        'debug');

class log
{
     // object instance
    private static $instance;

    private function __construct() {}

    private function __clone() {}

    public static function get_instance()
    {
        if (self::$instance === NULL)
        {
            self::$instance = new self;
        }

        return self::$instance;
    }

    public function write($file, $level, $user, $message)
    {
        $file = @fopen(CFG_LOG_DIR.$file,'a+');

        if ($file === FALSE)
        {
            return FALSE;
        }

        $message = trim(preg_replace("/([\s]+)/i", " ", $message));

        fwrite($file, '['.@date('Y.m.d H:i:s').'] ['.$level.'] ['.$user.'] '.$message."\n");

        return fclose($file);
    }
}

